package com.ten.jinxiancun.controller;

import com.ten.jinxiancun.entity.User;
import com.ten.jinxiancun.server.impl.userServerImpl;
import com.ten.jinxiancun.utils.MD5Util;
import com.ten.jinxiancun.utils.Result;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/user")
public class userController {
    @Autowired
    userServerImpl usl;
@RequestMapping("/getUserAll")
    public Object getUserAll(@RequestParam Map<String, Object> map) {
        Integer page = Integer.parseInt(map.get("page").toString());
        Integer limit = Integer.parseInt(map.get("limit").toString());
        map.put("limit", limit);
        map.put("page", (page - 1) * limit);
        List<Map<String, Object>> user = usl.getAlluser(map);
        int count = usl.userCount();
        Result res = new Result("0", "true", count, user);
        return res;


    }
    /*
    * 添加
    * */

    @RequestMapping("/addUser")
    @ResponseBody
    public Object addUser(User user) {
        user.setPassword(MD5Util.getMD5(user.getPassword()));
        int i = usl.AddUserFind(user);
        Result res = new Result("0", "true", i, true);
        return res;
    }
    @RequestMapping("/updateUser")
    @ResponseBody
    public Object updateUsers(@RequestParam Map<String, Object> map) {
        int i = usl.UpdateUserFind(map);
        Result res = new Result("0", "true", i, true);
        return res;
    }

    @RequestMapping("/deleteUser")
    @ResponseBody
    public Object deleteUsers(@RequestParam("id") Integer id) {
        int i = usl.DelectUserFind(id);
        Result res = new Result("0", "true", i, true);
        return res;
    }

    /*
    * 下拉框
    * */
    @RequestMapping("/UserType")
    public Object UserType(@RequestParam Map<String, Object> map) {

        Result res = new Result("0", "true", 0, usl.UserType(map));
        return res;


    }
        /*
               穿梭框 控制器
         * */

    @RequestMapping("/getRolesCSK")
    public Object getRolesCSK(@RequestParam Map<String, Object> m) {
        Result res = new Result("0", "true", 10, usl.getRolesFind(m));
        return res;
    }

    @RequestMapping("/getUserRolesCSK")
    public Object getUserRolesCSK(@RequestParam Map<String, Object> m) {
        Result res = new Result("0", "true", usl.getUserRolesFind(m).size(),usl.getUserRolesFind(m));
        return res;
    }

    @RequestMapping("/addRolesCSK")
    public Object addRolesCSK(@RequestParam Map<String, Object> m) {
        int i = usl.addRolesFind(m);
        Result res = new Result("0", "true", i, i);
        return res;
    }

    @RequestMapping("/deleteRoles")
    public Object deleteRoles(@Param("userId") Integer userId, @Param("roleId") Integer roleId) {
        int i = usl.deleteRolesFind(userId, roleId);
        Result res = new Result("0", "true", i, i);
        return res;
    }
}
